package com.zdf.license.web.controller;

import com.zdf.license.verify.annotation.VLicense;
import com.zdf.license.web.listener.Server1TimeVerifyListener;
import com.zdf.license.web.listener.UserNumVerifyListener;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
public class TestController {

    private final UserNumVerifyListener userNumVerifyListener;
    private final Server1TimeVerifyListener server1TimeVerifyListener;

    /**
     * 校验用户数量的小例子
     */
    private int userNum = 10;
    @RequestMapping("/1")
    public AjaxResult<?> test1(){
        try {
            boolean flag = userNumVerifyListener.verify(userNum + 1);
            if (flag) {
                userNum ++;
                return AjaxResult.success("校验通过。", userNum);
            }
            return AjaxResult.error("校验失败。", userNum);
        } catch (Exception e) {
            throw new RuntimeException("校验失败。" + e.getMessage());
        }
    }

    /**
     * 校验当前程序是否已经注册
     */
    @VLicense
    @RequestMapping("/2")
    public AjaxResult<?> test2(){
        return AjaxResult.success("已经注册");
    }

    /**
     * 校验xx是否已过期
     */
    @RequestMapping("/3")
    public AjaxResult<?> test3(){
        try {
            if (server1TimeVerifyListener.verify(new Date())) {
                return AjaxResult.success("时间尚未过期。");
            }
            return AjaxResult.error("时间已过期。");
        } catch (Exception e){
            throw new RuntimeException("校验失败。" + e.getMessage());
        }
    }

}
